package com.hd.quartz.common;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.Data;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

/**
 * <h3>partitionjob</h3>
 * <p></p>
 *
 * @author : hduong
 * @version : 1.0
 * @date : 2020-09-15 15:53
 **/
@Data
public class PageUtils implements Serializable {


    private static final long serialVersionUID = -7667513607740973513L;

    //分页数据
    //1: 总记录数
    //2: 每页记录数
    //3: 总页数
    //4: 当前页数
    //5: 列表数据 (实际返回数据)
    private long total;
    private long totalCount;
    private int pageSize;
    private long totalPage;
    private int currPage;
    private List<?> list;
    private List<?> rows;

    /**
     * 分页
     *
     * @param list       列表数据
     * @param totalCount 总记录数
     * @param pageSize   每页记录数
     * @param currPage   当前页数
     */
    public PageUtils(List<?> list, int totalCount, int pageSize, int currPage) {
        this.list = list;
        this.rows = this.list;
        this.totalCount = totalCount;
        this.total = totalCount;
        this.pageSize = pageSize;
        this.currPage = currPage;
        this.totalPage = (int) Math.ceil((double) totalCount / pageSize);
    }

    /*
     * 空数据的分页
     */
    public PageUtils(Map<String, Object> params) {
        Page page = new Query(params).getPage();
        new PageUtils(page);
    }


    /**
     * 分页
     */
    public PageUtils(Page<?> page) {
        this.list = page.getRecords();
        this.rows = this.list;
        this.totalCount = page.getTotal();
        this.total = this.totalCount;
        this.pageSize = (int) page.getSize();
        this.currPage = (int) page.getCurrent();
        this.totalPage = page.getPages();
    }

    /**
     * 分页
     */
    public PageUtils(IPage<?> iPage) {
        this.list = iPage.getRecords();
        this.rows = this.list;
        this.totalCount = iPage.getTotal();
        this.total = this.totalCount;
        this.pageSize = (int) iPage.getSize();
        this.currPage = (int) iPage.getCurrent();
        this.totalPage = iPage.getPages();
    }


}
